import React from "react";
import ReactDOM from "react-dom/client";
import "./sass/index.scss";
import App from "./App";

import "lib-flexible/flexible";

import { HashRouter } from "react-router-dom";

import { Provider } from "react-redux";

import store from "./store/store";

import axios from "axios";

import { Toast } from "react-vant";

import { urls } from "./urls/urls";

let appkey = "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=";

//添加请求基础路劲
axios.defaults.baseURL = "http://www.kangliuyong.com:10002";

//添加axios拦截器
//添加请求拦截器
axios.interceptors.request.use(
    function (config) {
        //在发送请求之前做些什么

        //

        //启动加载提示
        Toast({
            type: "loading",
            message: "加载中...",
            forbidClick: true,
            duration: 0,
        });

        //获取token
        let token = localStorage.getItem("token");

        //
        if (config.method === "get") {
            config.params = config.params || {};
            config.params.appkey = appkey;

            if (urls.indexOf(config.url) > -1) {
                config.params.tokenString = token;
            }
        } else if (config.method === "post") {
            //

            config.data = config.data || {};

            let dataString = "";
            for (let key in config.data) {
                dataString += `${key}=${config.data[key]}&`;
            }

            if (dataString) {
                dataString += `appkey=${appkey}`;
            }

            //

            if (urls.indexOf(config.url) > -1) {
                dataString += `&tokenString=${token}`;
            }

            config.data = dataString;
        }

        return config;
    },
    function (error) {
        // 对请求错误做些什么
        Toast.clear();
        return Promise.reject(error);
    }
);

// 添加响应拦截器
axios.interceptors.response.use(
    function (response) {
        // 2xx 范围内的状态码都会触发该函数。
        // 对响应数据做点什么
        Toast.clear();
        return response;
    },
    function (error) {
        // 超出 2xx 范围的状态码都会触发该函数。
        // 对响应错误做点什么
        Toast.clear();
        return Promise.reject(error);
    }
);

const root = ReactDOM.createRoot(
    document.getElementById("root")
);
root.render(
    // <React.StrictMode>
    <Provider store={store}>
        <HashRouter>
            <App />
        </HashRouter>
    </Provider>
    // </React.StrictMode>
);
